package cn.com.hbhgkj.bussiness;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.com.hbhgkj.common.DaoException;
import cn.com.hbhgkj.common.Pagination;
import cn.com.hbhgkj.common.ServiceException;
import cn.com.hbhgkj.config.Constants;
import cn.com.hbhgkj.dao.CopyrightRegisterationDao;
import cn.com.hbhgkj.pojo.CopyrightRegisteration;
import cn.com.hbhgkj.utils.CopyrightSender;
import cn.com.hbhgkj.utils.DateUtils;

public class CopyrightRegisterationServiceImpl implements
		CopyrightRegisterationService {

	public void updateCopyrightReg(CopyrightRegisteration cr)
			throws ServiceException {
		try {
			getCopyrightRegisterationDao().update(cr);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	// 从软协中心同步登记数据
	public void syncFromCopyright() throws ServiceException {
		try {
			// send form to copyright center
			CopyrightSender.login(Constants.AGENT_USER_NAME,
					Constants.AGENT_PASSWORD);
			List<CopyrightRegisteration> data = CopyrightSender
					.getData(DateUtils.getCurDate(), DateUtils
							.getSpecifiedDayAfter(new Date().toLocaleString()));

			List<CopyrightRegisteration> entitiesToSave = filterDuplicateData(data);

			if (entitiesToSave.size() > 0)
				getCopyrightRegisterationDao().saveOrUpdateAll(entitiesToSave);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	private List<CopyrightRegisteration> filterDuplicateData(
			List<CopyrightRegisteration> data) throws ServiceException {
		List<CopyrightRegisteration> result = new ArrayList<CopyrightRegisteration>();
		for (CopyrightRegisteration each : data) {
			if (!isSeriaNumExist(each.getSerialNum())) {
				result.add(each);
			}
		}
		return result;
	}

	public boolean isSeriaNumExist(String seriaNum) throws ServiceException {
		try {
			return copyrightRegisterationDao.findBySeriaNum(seriaNum).size() > 0 ? true
					: false;
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public List<CopyrightRegisteration> findLikeCopyrighter(String copyrighter,
			Pagination pagination) throws ServiceException {
		try {
			return copyrightRegisterationDao.findLikeCopyrighter(copyrighter,
					pagination);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public void saveCopyrightReg(CopyrightRegisteration cr)
			throws ServiceException {
		try {
			copyrightRegisterationDao.save(cr);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public List<CopyrightRegisteration> getAll(Pagination pagination)
			throws ServiceException {
		try {
			return copyrightRegisterationDao.getAll(pagination);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	private CopyrightRegisterationDao copyrightRegisterationDao;

	public CopyrightRegisterationDao getCopyrightRegisterationDao() {
		return copyrightRegisterationDao;
	}

	public void setCopyrightRegisterationDao(
			CopyrightRegisterationDao copyrightRegisterationDao) {
		this.copyrightRegisterationDao = copyrightRegisterationDao;
	}

}
